package com.webserver6.controllor;

import com.webserver6.annotations.Controller;
import com.webserver6.http.HttpServletRequest;
import com.webserver6.http.HttpServletResponse;
import com.webserver6.util.DBUtil;

import java.io.File;
import java.sql.*;

@Controller
public class UserController {
    private static File userDir;
    static {
        userDir = new File("./users");
        if (!userDir.exists()){
            userDir.mkdirs();
        }
    }
    public void reg(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始出来注册！！！！");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        if (username==null||username.isEmpty()||
        password==null||password.isEmpty()||
        nickname==null||nickname.isEmpty()||
        ageStr==null||ageStr.isEmpty()||!ageStr.matches("[0-9]+")
        ){
            response.sendRedirect("/reg_info_error.html");
            return;
        }
        System.out.println(username+" , "+password+" , "+nickname+" , "+ageStr);
        int age = Integer.parseInt(ageStr);
        try(
                Connection connection = DBUtil.getConnection();
                ){
            Statement statement = connection.createStatement();
            String sql = "SELECT 1 FROM userinfo WHERE username='"+username+"'";
            ResultSet rs = statement.executeQuery(sql);
            if (rs.next()){
                response.sendRedirect("/have_user.html");
                return;
            }
            sql = "INSERT INTO userinfo (username,password,nickname,age) "+"VALUES(')"+username+"','"
                    +password+"','"+nickname+"','"+age+")";
            System.out.println(sql);
            int num = statement.executeUpdate(sql);
            if (num>0){
                response.sendRedirect("/reg_success.html");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void login(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始处理登录！！！");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username+","+password);
        if (username==null||username.trim().isEmpty()||
        password==null||password.trim().isEmpty()){
            response.sendRedirect("login_info_error.html");
            return;
        }
        try (
                Connection connection = DBUtil.getConnection();
                ){
            String sql ="SELECT id,username,password,nickname,age" +
                    "FROM userinfo "+
                    "WHERE username=? "+
                    "AND password=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ResultSet rs = ps.executeQuery();
            if (rs.next()){
                response.sendRedirect("/login_success.html");
            }else {
                response.sendRedirect("/login_fail.html");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
